package wiki.hitime.stl.daq.service.factory.impl.check;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import wiki.hitime.stl.daq.bean.bo.DataAcquisitionValueBO;
import wiki.hitime.stl.daq.bean.bo.DataCheckConfigBO;
import wiki.hitime.stl.daq.bean.bo.DataCheckResultBO;
import wiki.hitime.stl.daq.bean.bo.DataCheckValueBO;
import wiki.hitime.stl.daq.enums.DataAcquisitionTypeEnum;
import wiki.hitime.stl.daq.enums.DataValueTypeEnum;
import wiki.hitime.stl.daq.service.factory.DataCheckFactory;
import wiki.hitime.stl.daq.util.StrUtils;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangdaoping
 * @create 2019-07-09 18:56
 */
@Service
public class SingleColumnRuleCheckImpl extends AbstractDataCheckService {


    /**
     * 获取实现类暴露的配置信息
     *
     * @return 数据采集类型
     */
    @Override
    public DataCheckConfigBO getCheckConfig() {
        return DataCheckConfigBO.builder()
                .supportAcquisitionType(DataAcquisitionTypeEnum.SINGLE_COLUMN_DATA.getStatus())
                .supportDataValueTypeList(Arrays.asList(
                        DataValueTypeEnum.NUMBER.getStatus()))
                .checkName("单列规则检查")
                .implServiceName(StrUtils.toLowerCase(this.getClass().getSimpleName(), 0))
                .build();
    }

    /**
     * 加载检查结果
     *
     * @param acquisitionValueBO 采集数据对象
     * @param dataCheckValueBO   检查数据对象
     * @return 检查结果对象
     */
    @Override
    public DataCheckResultBO loadCheckResult(DataAcquisitionValueBO acquisitionValueBO, DataCheckValueBO dataCheckValueBO) {
        List<String> checkExpressList = dataCheckValueBO.getDataList();
        List<String[]> dataStringArrayList = acquisitionValueBO.getDataList();
        if (CollectionUtils.isEmpty(checkExpressList)) {
            return DataCheckResultBO.builder().checkResult(Boolean.FALSE).checkSummary("检查表达式为空").build();
        }
        if (CollectionUtils.isEmpty(dataStringArrayList)) {
            return DataCheckResultBO.builder().checkResult(Boolean.FALSE).checkSummary("采集数据为空").build();
        }
        if (checkExpressList.size() != dataStringArrayList.size()) {
            return DataCheckResultBO.builder().checkResult(Boolean.FALSE).checkSummary("检查表达式和采集数据值不对应").build();
        }
        Boolean checkResult;
        for (int i = 0; i < checkExpressList.size(); i++) {
            String checkExpress = checkExpressList.get(i);
            String[] dataStringArray = dataStringArrayList.get(i);
            if (checkExpress.equals("*")) {
                continue;
            }
            Map<String, BigDecimal> valueMap = new HashMap<>();
            valueMap.put("value", super.valueFormatBigDecimal(dataStringArray[0]));
            BigDecimal result = super.getCalcResult(valueMap, checkExpress);
            checkResult = result.compareTo(BigDecimal.ZERO) > 0 ? Boolean.TRUE : Boolean.FALSE;
            if (!checkResult) {
                return DataCheckResultBO.builder().checkResult(Boolean.FALSE).checkSummary(checkExpress + "检查返回" + checkResult.toString()).build();
            }
        }
        return DataCheckResultBO.builder().checkResult(Boolean.TRUE).checkSummary("全部检查通过").build();
    }
}
